package com.fishing.controller;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.support.spring.stat.annotation.Stat;
import com.alibaba.fastjson.JSONObject;
import com.fishing.EnumEntity.Platformreceiving;
import com.fishing.entity.resource_log_reason;
import com.fishing.util.personal.GameStatFiled;
import com.fishing.util.personal.resource_log_reason_type;

import QL.ButlerGetGlobalResourceStatRequest;
import QL.ButlerGetGlobalResourceStatResponse;
import QL.ButlerResourceStatItem;
import QL.ButlerResourceStockItem;
import QL.QLClient;

@Controller
public class StatRoomController {

	private static QLClient client = new QLClient();
	
	@RequestMapping("room/stat")
	public ModelAndView stat(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("stat_Room");
		return mav;
	}
	
	@RequestMapping("room/stat2")
	public ModelAndView stat2(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("stat_Room2");
		return mav;
	}
	
	//region 跳转游戏统计页面
	@RequestMapping("room/gamestat")
	public ModelAndView gamestat(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("gamestat");
		return mav;
	}
	//endregion
	
	//region 房间数据统计
	@ResponseBody
	@RequestMapping(value="stat/RoomData",produces="text/html;charset=UTF8")
	public Map<String,Object> RoomData(HttpServletRequest request,int room) {
		List list = new ArrayList();
		ButlerGetGlobalResourceStatRequest req = new ButlerGetGlobalResourceStatRequest();
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		ButlerGetGlobalResourceStatResponse rsp = new ButlerGetGlobalResourceStatResponse();
		client.Execute(req, rsp);
		if(rsp.StockItems != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			resource_log_reason reason = new resource_log_reason();
			for(int i = 0;i < rsp.GamePlatformItems.size();i++) {
				if(rsp.GamePlatformItems.get(i).GameFlag == 1 &&  room == 1) {
					//捕鱼3D放水额度
					map.put("ThreeFishingWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
					//捕鱼3D抽水额度
					map.put("ThreeFishingWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
					//捕鱼3D平台收益
					map.put("ThreeFishingFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
					//捕鱼3D水位 
					map.put("ThreeFishingWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
					list.add(map);
				}else if(rsp.GamePlatformItems.get(i).GameFlag == 3 && room == 3){
					//水浒传放水额度
					map.put("ShzWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
					//水浒传抽水额度
					map.put("ShzWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
					//水浒传平台收益
					map.put("ShzFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
					//水浒传水位 
					map.put("ShzWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
					list.add(map);
				}else if(rsp.GamePlatformItems.get(i).GameFlag == 4 && room == 4) {
					//拉霸机放水额度
					map.put("LbjWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
					//拉霸机抽水额度
					map.put("LbjWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
					//拉霸机平台收益
					map.put("LbjFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
					//拉霸机水位 
					map.put("LbjWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
					//拉霸机彩金池
					map.put("LbjBonus", rsp.GamePlatformItems.get(i).Bonus);
					list.add(map);
				}else if(rsp.GamePlatformItems.get(i).GameFlag == 5 && room == 5) {
					//打地鼠放水额度
					map.put("DdsWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
					//打地鼠抽水额度
					map.put("DdsWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
					//打地鼠平台收益
					map.put("DdsFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
					//打地鼠水位 
					map.put("DdsWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
					//打地鼠彩金池
					map.put("DdsBonus", rsp.GamePlatformItems.get(i).Bonus);
					list.add(map);
				}else if(rsp.GamePlatformItems.get(i).GameFlag == 6 && room == 6) {
					//抢红包放水额度
					map.put("QHBWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
					//抢红包抽水额度
					map.put("QHBWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
					//抢红包平台收益
					map.put("QHBFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
					//抢红包水位 
					map.put("QHBWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
					list.add(map);
				}else if(rsp.GamePlatformItems.get(i).GameFlag == 7 && room == 7) {
					//龙虎斗放水额度
					map.put("LhdWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
					//龙虎斗抽水额度
					map.put("LhdWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
					//龙虎斗平台收益
					map.put("LhdFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
					//龙虎斗水位 
					map.put("LhdWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
					list.add(map);
				}
			}
			Map<String,Object> map2 = new  HashMap<String,Object>();
			map2.put("data", list);
			map2.put("count", list.size());
			map2.put("message", "");
			map2.put("status", "200");
			return map2;
		}else {
			Map<String,Object> map = new  HashMap<String,Object>();
			map.put("data", "");
			map.put("count", 0);
			map.put("message", "");
			map.put("status", "200");
			return map;
		}
	}
	//endregion
	
	//region 游戏数据统计
			@ResponseBody
			@RequestMapping(value="stat/gamedata",produces="text/html;charset=UTF8")
			public Map<String,Object> gamedata(HttpServletRequest request) {
				//定义变量
				Map<String,Object> map = new HashMap<>();
				Map<String,Object> map_data = new  HashMap<String,Object>();
				List list = new ArrayList<>();

				//发送数据请求
				ButlerGetGlobalResourceStatRequest req = new ButlerGetGlobalResourceStatRequest();
				req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
				ButlerGetGlobalResourceStatResponse rsp = new ButlerGetGlobalResourceStatResponse();
				if (req.ZoneId < 0)
					client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
				client.Execute(req, rsp);
				
				//region 筛选数据(先遍历rsp.StatItems中的reason，然后与resource_log_reason_type进行比对,因为只统计金币  所以要在后面加个条件，名字是金币)
				if(rsp.StockItems != null){
					for(ButlerResourceStatItem statitem:rsp.StatItems) {
						GameStatFiled gamename = new GameStatFiled();
						for(resource_log_reason_type reason : EnumSet.allOf(resource_log_reason_type.class)){
							if(reason.getValue() == (int)statitem.Reason && statitem.Name.equals("金币")) {
								if (statitem.Reason >=300 && statitem.Reason < 400) {
									if (statitem.TotalCount < 0) {
										gamename.Three3Dfishingtransport = gamename.Three3Dfishingtransport + statitem.TotalCount;
										map.put("Three3Dfishingtransport", Math.abs(gamename.Three3Dfishingtransport));
										break;
									}else {
										gamename.Three3Dfishingwin = gamename.Three3Dfishingwin + statitem.TotalCount;
										map.put("Three3Dfishingwin", "-" + gamename.Three3Dfishingwin);
										break;
									}
								}else if (statitem.Reason >=400 && statitem.Reason < 420) {
									if (statitem.TotalCount < 0) {
										gamename.Three2Dfishingtransport = gamename.Three2Dfishingtransport + statitem.TotalCount;
										map.put("Three2Dfishingtransport", Math.abs(gamename.Three2Dfishingtransport));
										break;
									}else {
										gamename.Three2Dfishingwin = gamename.Three2Dfishingwin + statitem.TotalCount;
										map.put("Three2Dfishingwin", "-" + gamename.Three2Dfishingwin);
										break;
									}
								}else if (statitem.Reason >=420 && statitem.Reason < 440) {
									if (statitem.TotalCount < 0) {
										gamename.Sbbytransport = gamename.Sbbytransport + statitem.TotalCount;
										map.put("Sbbytransport", Math.abs(gamename.Sbbytransport));
										break;
									}else {
										gamename.Sbbywin = gamename.Sbbywin + statitem.TotalCount;
										map.put("Sbbywin", "-" + gamename.Sbbywin);
										break;
									}
								}else if (statitem.Reason >=440 && statitem.Reason < 460) {
									if (statitem.TotalCount < 0) {
										gamename.Chaofanfishingtransport = gamename.Chaofanfishingtransport + statitem.TotalCount;
										map.put("Chaofanfishingtransport", Math.abs(gamename.Chaofanfishingtransport));
										break;
									}else {
										gamename.Chaofanfishingwin = gamename.Chaofanfishingwin + statitem.TotalCount;
										map.put("Chaofanfishingwin", "-" + gamename.Chaofanfishingwin);
										break;
									}
								}else if (statitem.Reason >=460 && statitem.Reason < 500) {
									if (statitem.TotalCount < 0) {
										gamename.Shenhaifishingtransport = gamename.Shenhaifishingtransport + statitem.TotalCount;
										map.put("Shenhaifishingtransport", Math.abs(gamename.Shenhaifishingtransport));
										break;
									}else {
										gamename.Shenhaifishingwin = gamename.Shenhaifishingwin + statitem.TotalCount;
										map.put("Shenhaifishingwin", "-" + gamename.Shenhaifishingwin);
										break;
									}
								}else if (statitem.Reason >=500 && statitem.Reason < 600) {
									if (statitem.TotalCount < 0) {
										gamename.Shztransport = gamename.Shztransport + statitem.TotalCount;
										map.put("Shztransport", Math.abs(gamename.Shztransport));
										break;
									}else {
										gamename.Shzwin = gamename.Shzwin + statitem.TotalCount;
										map.put("Shzwin", "-" + gamename.Shzwin);
										break;
									}
								}else if (statitem.Reason >=600 && statitem.Reason < 700) {
									if (statitem.TotalCount < 0) {
										gamename.Lbjtransport = gamename.Lbjtransport + statitem.TotalCount;
										map.put("Lbjtransport", Math.abs(gamename.Lbjtransport));
										break;
									}else {
										gamename.Lbjwin = gamename.Lbjwin + statitem.TotalCount;
										map.put("Lbjwin", "-" + gamename.Lbjwin);
										break;
									}
								}else if (statitem.Reason >=700 && statitem.Reason < 900) {
									if (statitem.TotalCount < 0) {
										gamename.Ddstransport = gamename.Ddstransport + statitem.TotalCount;
										map.put("Ddstransport", Math.abs(gamename.Ddstransport));
										break;
									}else {
										gamename.Ddswin = gamename.Ddswin + statitem.TotalCount;
										map.put("Ddswin", "-" + gamename.Ddswin);
										break;
									}
								}else if (statitem.Reason >=900 && statitem.Reason < 1000) {
									if (statitem.TotalCount < 0) {
										gamename.Qhbtransport = gamename.Qhbtransport + statitem.TotalCount;
										map.put("Qhbtransport", Math.abs(gamename.Qhbtransport));
										break;
									}else {
										gamename.Qhbwin = gamename.Qhbwin + statitem.TotalCount;
										map.put("Qhbwin", "-" + gamename.Qhbwin);
										break;
									}
								}else if (statitem.Reason >=1000 && statitem.Reason < 1100) {
									if (statitem.TotalCount < 0) {
										gamename.Lhdtransport = gamename.Lhdtransport + statitem.TotalCount;
										map.put("Lhdtransport", Math.abs(gamename.Lhdtransport));
										break;
									}else {
										gamename.Lhdwin = gamename.Lhdwin + statitem.TotalCount;
										map.put("Lhdwin", "-" + gamename.Lhdwin);
										break;
									}
								}else if (statitem.Reason >=1100 && statitem.Reason < 1200) {
									if (statitem.TotalCount < 0) {
										gamename.Sgmltransport = gamename.Sgmltransport + statitem.TotalCount;
										map.put("Sgmltransport", Math.abs(gamename.Sgmltransport));
										break;
									}else {
										gamename.Sgmlwin = gamename.Sgmlwin + statitem.TotalCount;
										map.put("Sgmlwin", "-" + gamename.Sgmlwin);
										break;
									}
								}else if (statitem.Reason >=1200 && statitem.Reason < 1300) {
									if (statitem.TotalCount < 0) {
										gamename.Xgnltransport = gamename.Xgnltransport + statitem.TotalCount;
										map.put("Xgnltransport", Math.abs(gamename.Xgnltransport));
										break;
									}else {
										gamename.Xgnlwin = gamename.Xgnlwin + statitem.TotalCount;
										map.put("Xgnlwin", "-" + gamename.Xgnlwin);
										break;
									}
								}else if (statitem.Reason >=1300 && statitem.Reason < 1320) {
									if (statitem.TotalCount < 0) {
										gamename.Lmfztransport = gamename.Lmfztransport + statitem.TotalCount;
										map.put("Lmfztransport", Math.abs(gamename.Lmfztransport));
										break;
									}else {
										gamename.Lmfzwin = gamename.Lmfzwin + statitem.TotalCount;
										map.put("Lmfzwin", "-" + gamename.Lmfzwin);
										break;
									}
								}else if (statitem.Reason >=1320 && statitem.Reason < 1400) {
									if (statitem.TotalCount < 0) {
										gamename.Bjltransport = gamename.Bjltransport + statitem.TotalCount;
										map.put("Bjltransport", Math.abs(gamename.Bjltransport));
										break;
									}else {
										gamename.Bjlwin = gamename.Bjlwin + statitem.TotalCount;
										map.put("Bjlwin", "-" + gamename.Bjlwin);
										break;
									}
								}else if (statitem.Reason >=1400 && statitem.Reason < 1500) {
									if (statitem.TotalCount < 0) {
										gamename.Nntransport = gamename.Nntransport + statitem.TotalCount;
										map.put("Nntransport", Math.abs(gamename.Nntransport));
										break;
									}else {
										gamename.Nnwin = gamename.Nnwin + statitem.TotalCount;
										map.put("Nnwin", "-" + gamename.Nnwin);
										break;
									}
								}else if (statitem.Reason >=1500 && statitem.Reason < 1600) {
									if (statitem.TotalCount < 0) {
										gamename.Mjtransport = gamename.Mjtransport + statitem.TotalCount;
										map.put("Mjtransport", Math.abs(gamename.Mjtransport));
										break;
									}else {
										gamename.Mjwin = gamename.Mjwin + statitem.TotalCount;
										map.put("Mjwin", "-" + gamename.Mjwin);
										break;
									}
								}else if (statitem.Reason >=1600 && statitem.Reason < 1700) {
									if (statitem.TotalCount < 0) {
										gamename.Ddztransport = gamename.Ddztransport + statitem.TotalCount;
										map.put("Ddztransport", Math.abs(gamename.Ddztransport));
										break;
									}else {
										gamename.Ddzwin = gamename.Ddzwin + statitem.TotalCount;
										map.put("Ddzwin", "-" + gamename.Ddzwin);
										break;
									}
								}else if (statitem.Reason >=1700 && statitem.Reason < 1800) {
									if (statitem.TotalCount < 0) {
										gamename.Jjhfishingtransport = gamename.Jjhfishingtransport + statitem.TotalCount;
										map.put("Jjhfishingtransport", Math.abs(gamename.Jjhfishingtransport));
										break;
									}else {
										gamename.Jjhfishingwin = gamename.Jjhfishingwin + statitem.TotalCount;
										map.put("Jjhfishingwin", "-" + gamename.Jjhfishingwin);
										break;
									}
								}else if (statitem.Reason >=1800 && statitem.Reason < 19800) {
									if (statitem.TotalCount < 0) {
										gamename.Zjhtransport = gamename.Zjhtransport + statitem.TotalCount;
										map.put("Zjhtransport", Math.abs(gamename.Zjhtransport));
										break;
									}else {
										gamename.Zjhwin = gamename.Zjhwin + statitem.TotalCount;
										map.put("Zjhwin", "-" + gamename.Zjhwin);
										break;
									}
								}
							}
						}
					}
					//返回数据
					list.add(map);
					map_data.put("data", list);
					map_data.put("count", 1);
					map_data.put("message", "");
					map_data.put("status", "200");
					return map_data;
				}
				map_data.put("data", "");
				map_data.put("count", 0);
				map_data.put("message", "");
				map_data.put("status", "200");
				return map_data;
			}
			//endregion
}
